﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LeadCRM.Models;

namespace LeadCRM.Controllers
{
    public class MastersController : Controller
    {
        #region CustomerType Master
        //
        // GET: /Masters/CustomerType
        [HttpGet]
        public ActionResult CustomerType()
        {
            


            LeadCRM.Entities.LeadCRMContext db = new Entities.LeadCRMContext();
            var data = from p in db.CustomerTypeMaster
                       where p.IsDeleted == false
                       select p;

            ViewBag.CustomerType = data;
            return View();
        }

        [HttpPost]
        public ActionResult CustomerType(AddCustomerType customerType)
        {
            LeadCRM.Entities.LeadCRMContext db = new Entities.LeadCRMContext();
            if (customerType.Command.Equals("Add", StringComparison.OrdinalIgnoreCase)
                && !String.IsNullOrWhiteSpace(customerType.CustomerTypeName)
                )
            {

                if (customerType.CustomerTypeID == 0)
                {
                    //Check if a CustomerType Exsist in the DB
                    if ((from p in db.CustomerTypeMaster
                         where p.CustomerTypeName.ToLower().Equals(customerType.CustomerTypeName)
                         select p).Count() > 0)
                    {
                        //Set Error Msg
                        ViewBag.Error = "Customer Type with this name already exists. Please enter some other type";
                    }
                    else
                    {
                        Entities.CustomerTypeMaster cus = new Entities.CustomerTypeMaster()
                            {
                                CustomerTypeName = customerType.CustomerTypeName,
                                CreatedOn = DateTime.Now,
                                CreatedBy = 0,
                                IsDeleted = false,
                                LastModifiedBy = null,
                                LastModifiedOn = null
                            };
                        db.CustomerTypeMaster.Add(cus);
                        db.SaveChanges();
                        ViewBag.MsgSuccess = "Sucessfully Added New Customer Type";

                    }

                    //Add a new CustomerType
                }
                else
                {
                    //Edit the CustomerType
                    //Check if a CustomerType Exsist in the DB
                    if ((from p in db.CustomerTypeMaster
                         where p.CustomerTypeName.ToLower().Equals(customerType.CustomerTypeName) &&
                         p.ID != customerType.CustomerTypeID
                         select p).Count() > 0)
                    {
                        //Set Error Msg
                        ViewBag.Error = "Customer Type with this name already exists. Please enter some other type";
                    }
                    else
                    {
                        Entities.CustomerTypeMaster cus = (from p in db.CustomerTypeMaster
                                                           where p.ID == customerType.CustomerTypeID
                                                           select p).First();
                        cus.CustomerTypeName = customerType.CustomerTypeName;

                        cus.IsDeleted = false;
                        cus.LastModifiedBy = 0;
                        cus.LastModifiedOn = DateTime.Now;



                        db.SaveChanges();
                        ViewBag.MsgSuccess = "Sucessfully updated Customer Type";
                    }
                }
            }
            else if (customerType.Command.Equals("Delete", StringComparison.OrdinalIgnoreCase))
            {
                var customer = from p in db.CustomerTypeMaster
                               where p.ID == customerType.CustomerTypeID
                               select p;
                if (customer.Count() > 0)
                {
                    Entities.CustomerTypeMaster cus = customer.First();
                    cus.IsDeleted = true;
                    db.SaveChanges();
                    ViewBag.MsgSuccess = "Customer Type Deleted";
                }
                else
                {
                    ViewBag.Error = "Customer Type with ID=" + customerType.CustomerTypeID.ToString() + " not found";
                }

            }
            else if (String.IsNullOrWhiteSpace(customerType.CustomerTypeName))
            {
                ViewBag.Error = "Customer Type cannot be blank";
            }
            var data = from p in db.CustomerTypeMaster
                       where p.IsDeleted == false
                       select p;

            ViewBag.CustomerType = data;
            return View();
        }

        #endregion

        #region Menu Master

        //
        // GET: /Masters/MenuMaster
        [HttpGet]
        public ActionResult MenuMaster()
        {
            LeadCRM.Entities.LeadCRMContext db = new Entities.LeadCRMContext();
            var data = from p in db.Menu
                       where p.isDelete == false
                       orderby p.OrderNo
                       select p
                       ;

            ViewBag.MenuData = data;
            
            return View();
        }

        [HttpGet]
        public ActionResult EditMenuMaster(int ID)
        {
            LeadCRM.Entities.LeadCRMContext db = new Entities.LeadCRMContext();
            var data = from p in db.Menu
                       where p.isDelete == false &
                       p.MenuID == ID
                       select p;

            if (data.Count() > 0)
            {
                ViewBag.MenuData = data.First();
            }            

            return View("EditMenuMaster");
        }

        [HttpPost]
        public ActionResult EditMenuMaster(LeadCRM.Models.Menu menu,int ID)
        {
            LeadCRM.Entities.LeadCRMContext db = new Entities.LeadCRMContext();
            var data = from p in db.Menu
                       where p.isDelete == false &
                       p.MenuID == ID
                       select p;

            if (data.Count() > 0)
            {
                var menuInfo = data.First();
                menuInfo.Description = menu.MenuDesc;
                menuInfo.MenuName = menu.MenuName;
                menuInfo.MenuURL = menu.MenuURL;
                db.SaveChanges();
            }

            return RedirectToAction("MenuMaster","Masters");
        }

        #endregion
    }
}
